package com.jinlejia.erp.ui.mine.contract;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityCheckSupplementAgreementBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

/**
 * 查看补充协议
 */
public class CheckSupplementAgreementActivity extends AppCompatActivity {


    private ActivityCheckSupplementAgreementBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_check_supplement_agreement);
        binding.setEvent(this);
        WebSettings webSettings = binding.webview.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.supportMultipleWindows();
        webSettings.setAllowContentAccess(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);

        binding.webview.setWebChromeClient(new WebChromeClient());
        getData();
    }

    public void onViewClicked(View view) {
        finish();
    }

    private void getData() {
        OkHttpUtils.get()
                .url(ApiConfig.SUPPLY_AGREEMENT_TEXT)
                .addParams("id", getIntent().getStringExtra("id"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(CheckSupplementAgreementActivity.this, response)) {
                            String url = JSON.parseObject(response.get("data").toString()).getString("htmlUrl");
                            binding.webview.loadUrl(ApiConfig.MAGEHEADER_URL().concat(url));
                        } else {
                            binding.webview.loadUrl("");
                        }
                    }
                });
    }
}
